import requests
from bs4 import BeautifulSoup
import tkinter as tk
from PIL import Image,ImageTk
import io
import pickle
from tkinter import messagebox

h = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}

r = requests.get('http://www.xxsy.net/search' , headers=h)
c = r.text
#print(c)

soup = BeautifulSoup(c,'html.parser')
pages = soup.find('div',{'class':'pages'}).find_all('a')[-3].text


comic_list = []
for i in range(1,int(pages) + 1):
    url = 'http://www.xxsy.net/search?s_wd=&sort=9&pn=' + str(i)
    r_p = requests.get(url , headers=h)
    c_p = r_p.text

    soup_p = BeautifulSoup(c_p,'html.parser')

    search_list = soup.find('div',{'class':'result-list'}).find_all('li')
    for item in search_list:
        comic = {}
        comic['title'] = item.find('div',{'class':'info'}).find('h4').find('a').text
        x = item.find('span',{'class':'subtitle'}).find_all('a')
        comic['author'] = x[0].text
        comic['style'] = x[1].text
        comic['zhuangtai'] = item.find('span',{'class':'subtitle'}).find('span').text
        comic['intro'] = item.find('p',{'class':'detail'}).text
        comic['img'] = item.find('a',{'class':'book commonbook'}).find('img')['data-src']
        comic_list.append(comic)
#print(comic_list)


def showInfo(i):
    item = comic_list[i]
    Url = 'http:' + item['img']
    imgR = requests.get(Url)
    imgC = imgR.content
    data_stream = io.BytesIO(imgC)
    img = Image.open(data_stream)
    photo = ImageTk.PhotoImage(img)
    imgLabel.config(image=photo)
    imgLabel.image = photo

    titleLabel.config(text=comic_list[i]['title'])
    authorLabel.config(text=comic_list[i]['author'])
    styleLabel.config(text=comic_list[i]['style'])
    zhuangtaiLabel.config(text=comic_list[i]['zhuangtai'])
    introLabel.config(text=comic_list[i]['intro'])



def login():
    name = nameValue.get()
    password = passwordValue.get()
    try:
        with open('usrInfo.pickle','rb') as usr_file:
            usrInfo = pickle.load(usr_file)
    except Exception as e:
        with open('usrInfo.pickle','wb') as usr_file:
            usrInfo = {'admin':'admin'}
            pickle.dump(usrInfo,usr_file)
    if name in usrInfo:
        if usrInfo[name] == password:
            tk.messagebox.showinfo(title='欢迎!',message='%s 用户欢迎你！'% name)
        else:
            tk.messagebox.showerror(title='错误!',message='密码错误请重试!')
    else:
        yn = tk.messagebox.askyesno(title='错误!',message='用户不存在，您是否现在注册?')
        if yn:
            signUp()

def signUp():#登陆界面的
    def ok():
        name = signUp_nameValue.get()
        passwd = signUp_passwordValue.get()
        confirmPasswd = confirm_passwordValue.get()

        try:
            with open('usrInfo.packle','rb') as usr_file:
                usrInfo = pickle.load(usr_file)
        except Exception as e:
            with open('usrInfo.pickle','wb') as usr_file:
                usrInfo = {'admin':'admin'}
                pickle.dump(usrInfo,usr_file)
        if name != '':#判断用户名是否为空
            if name in usrInfo:
                tk.messagebox.showinfo(title='提示',message='%s 此用户已经存在'% name)
            else:
                if passwd != '' and passwd == confirmPasswd:
                    usrInfo[name] = passwd
                    print(usrInfo)
                    with open('usrInfo.pickle','wb') as usr_file:
                        pickle.dump(usrInfo,usr_file)
                    tk.messagebox.showinfo(title='恭喜您加入潇湘书院',message='%s，您已注册成功!' % name)
                    signUp_win.destroy()
                else:
                    tk.messagebox.showerror(title='错误',message='密码不能为空或者请重新确认密码')
        else:
            tk.messagebox.showerror(title='错误',message='用户名不能为空')
    def cancel():
        signUp_win.destroy()


    signUp_win = tk.Toplevel(window)
    signUp_win.title('注册')
    signUp_win.geometry('300x200')

    tk.Label(signUp_win,text='用户名:').place(x=30,y=20)
    signUp_nameValue = tk.StringVar()
    tk.Entry(signUp_win,textvariable=signUp_nameValue).place(x=100,y=20)

    tk.Label(signUp_win,text='密  码:').place(x=30,y=60)
    signUp_passwordValue = tk.StringVar()
    tk.Entry(signUp_win,textvariable=signUp_passwordValue,show='*').place(x=100,y=60)

    tk.Label(signUp_win,text='确认密码;').place(x=30,y=100)
    confirm_passwordValue = tk.StringVar()
    tk.Entry(signUp_win,textvariable=confirm_passwordValue,show='*').place(x=100,y=100)

    tk.Button(signUp_win,text='确定',command=ok).place(x=110,y=140)
    tk.Button(signUp_win,text='取消',command=cancel).place(x=160,y=140)
    #登录界面显示框
window = tk.Tk()
window.title('潇湘书院')
window.geometry('400x300')


nameLbl = tk.Label(window,text='用户名:')
nameLbl.place(x=100,y=50)

nameValue = tk.StringVar()
tk.Entry(window,textvariable=nameValue).place(x=150,y=50)

tk.Label(window,text='密 码:').place(x=105,y=100)
passwordValue = tk.StringVar()
tk.Entry(window,textvariable=passwordValue,show='*').place(x=150,y=100)

tk.Button(window,text='登录',command=login).place(x=150,y=150)
tk.Button(window,text='注册',command=signUp).place(x=210,y=150)

window.mainloop()


def next():
    global index
    index += 1
    showInfo(index)

def shang():
    global index
    index -=1
    showInfo(index)

window = tk.Tk()
window.title('潇湘书院')
window.geometry('500x550')

index=0

imgLabel = tk.Label(window,bg='gray')
imgLabel.pack()

titleLabel = tk.Label(window)
titleLabel.pack()

authorLabel = tk.Label(window)
authorLabel.pack()

styleLabel = tk.Label(window)
styleLabel.pack()

zhuangtaiLabel = tk.Label(window)
zhuangtaiLabel.pack()

introLabel = tk.Label(window,wraplength=450)
introLabel.pack()


tk.Button(window,text='上一页',command=shang).place(x=150,y=500)
tk.Button(window,text='下一页',command=next).place(x=300,y=500)

showInfo(index)

window.mainloop()
